package com.zsrt.oa.company.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.oa.company.entity.CompanyEntity;
import com.zsrt.oa.company.service.CompanyService;
import com.zsrt.oa.company.service.impl.CompanyServiceImpl;
import com.zsrt.utils.DBUtils;

/**
 * 公司的控制层
 * @author 罗智壕-13424431416
 *
 * 2017年6月19日
 */
public class CompanyController extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 初始化公司的服务层
	 */
	private CompanyService service = new CompanyServiceImpl();
       
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String method = request.getParameter("method");
		if(null != method && !"".equals(method.trim())){
			
			Map<String, Object> map = new HashMap<>();
			
			//公司的增加
			if("add".equals(method)){
				
				CompanyEntity entity = new CompanyEntity();
				entity.setCompanyName(request.getParameter("companyName"));
				entity.setCompanyTel(request.getParameter("companyTel"));
				entity.setCompanyManager(request.getParameter("companyManager"));
				
				service.add(entity, map);
			
			//查找全部公司
			}else if("findAll".equals(method)){
				
				service.findAll(map);
				
			//批量删除
			}else if("batchDeleteIds".equals(method)){
				String companyIds = request.getParameter("companyIds");
				
				service.batchDeleteIds(companyIds, map);
				
			//指定查询
			}else if("findById".equals(method)){
				String id = request.getParameter("id");
				
				service.findById(id, map);
				
			//修改
			}else if("updateProductMessage".equals(method)){
				CompanyEntity entity = new CompanyEntity();
				String id = request.getParameter("id");
				if(null != id || !"".equals(id)){
					entity.setId(Integer.parseInt(id));
				}
				entity.setCompanyName(request.getParameter("companyName"));
				entity.setCompanyTel(request.getParameter("companyTel"));
				entity.setCompanyManager(request.getParameter("companyManager"));
				service.updateCompanyMessage(entity, map);
			}
			
			DBUtils.toJson(map, response);
		}
	}
}
